package com.leyou.page.qurtz;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.leyou.item.client.ItemClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Job {

        @Autowired
        private StringRedisTemplate redisTemplate;
        @Autowired
        private ItemClient itemClient;
    @Scheduled(fixedDelay = 1000) //每10秒检查 一次目录导航栏是否有更新；
    public void checkCategory() throws Exception {

            //定时任务开启了；
            System.out.println("定时任务开始了");
            String category = redisTemplate.opsForValue().get("all:category");
            if (category!=null&&!category.equals("")) return;

            //如果为空
        List<Map<String, Object>> allCategory = itemClient.findAllCategory();

        redisTemplate.opsForValue().set("all:category",new ObjectMapper().writeValueAsString(allCategory),24, TimeUnit.HOURS);
    }


}
